//
//  AddWorkAummaryViewController.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 16/12/20.
//  Copyright © 2016年 com.xtion.crm. All rights reserved.
//

#import "AddWorkSummaryViewController.h"
#import "WorkSummaryInfo.h"
#import "WorkSummaryDynamicCellConstraintFactory.h"

NSString *const AddWorkSummaryViewControllerTitleParamKey = @"AddWorkSummaryViewControllerTitleParamKey";

@interface AddWorkSummaryViewController ()
{
    
}
@end

@implementation AddWorkSummaryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self checkSession];
    
    NSString *title = self.param[AddWorkSummaryViewControllerTitleParamKey]; 
    [self setBackNavigateItemTitle:title];
    
    _vclType = DynamicColumnFieldViewTypeAdd;
    _transformMap = [WorkSummaryInfo JSONObjectProperty:nil];
    _constraint = [WorkSummaryDynamicCellConstraintFactory factoryForModify:nil];
    
    [super initViewDidLoad];
    [super reloadDyanmicField];

}

-(void)clickedRightBarButton:(id)sender
{
    [UIViewController closeKeyBoard];
    
    if([super verifyDynamicField:_constraint] == YES)
    {
        WorkSummaryInfo *modelInfo = [WorkSummaryInfo emptyWorkSummaryInfo];
        
        XMSQueueDictionary *qdSet = _transformMap;
        
        [super extractDynamicFieldValue:_constraint transformMap:qdSet packObj:modelInfo specialExtract:^BOOL(FieldDescript *fixField)
         {
             return NO;
         }];
        
        [_manager addWorkSummary:modelInfo completion:^(NSInteger nResult, BOOL bReslut, NSError *error)
         {
             [super filterRequestResult:nResult defaultError:error noFilter:^(NSInteger requestResult)
              {
                  switch (nResult)
                  {
                      case YES:
                      {
                          if(bReslut == YES)
                          {
                              [XUIAlertView alertViewWithTitle:@"提示" message:@"保存成功" cancelButtonTitle:nil otherButtonTitles:@[@"确定"] clickBlock:^(NSInteger nClickindex)
                               {
                                   [XUINavigationController pop];
                               }];
                          }
                          else
                          {
                              if(error.domain.length > 0)
                                  [UIAlertView showErrorMsg:error.domain];
                              else
                                  [UIAlertView showErrorMsg:@"保存失败"];
                          }

                          break;
                      }
                      default:
                      {
                          [UIAlertView showErrorMsg:@"保存失败"];
                          break;
                      }
                  }
              }];
         }];
    }
}

- (CGFloat)dynamicColumnFieldCore:(DynamicColumnFieldCore *)dynamicColumnFieldCore heightForHeaderInGroup:(NSInteger)group
{
    return 10;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
